/*
    * Author:   Sander
    * Date:     13-mei-2011
    * Name:     Utils
    * Version:  1.0
    *
    * This code is property of Sander blahblahblah copyright blahblah.
*/


package javaraytracer;

//PENDING:add function/property description
public class Utils
{

	public static int parseInt(String string)
	{
		try
		{
			return Integer.parseInt(string);
		}
		catch(NumberFormatException e)
		{
			return -1;
		}
	}
	
	public static double interpolate(double a, double b, double u)
	{
		return a + (b - a) * u;
	}
	
	public static Vec3 interpolate(Vec3 a, Vec3 b, double u)
	{
		return new Vec3(a.x + (b.x - a.x) * u,
						a.y + (b.y - a.y) * u,
						a.z + (b.z - a.z) * u);
	}
	
	public static Vec3 interpolate(Vec3 a, Vec3 b, Vec3 c, double u, double v)
	{
		return new Vec3(a.x + (b.x - a.x) * u + (c.x - a.x) * v,
						a.y + (b.y - a.y) * u + (c.y - a.y) * v,
						a.z + (b.z - a.z) * u + (c.z - a.z) * v);
	}
	
	public static Vec3 wrapUVW(Vec3 uvw)
	{
		while(uvw.x > 1.0) uvw.x -= 1.0;
		while(uvw.y > 1.0) uvw.y -= 1.0;
		while(uvw.z > 1.0) uvw.z -= 1.0;
		return uvw;
	}
	
	public static Vec3 createLookAt(Vec3 pos1, Vec3 pos2)
	{
		return createLookAt(pos1, pos2, 0);
	}
	
	public static Vec3 createLookAt(Vec3 pos1, Vec3 pos2, double roll)
	{
		Vec3 d = pos2.sub(pos1);
		double pitch, yaw;
		pitch = -Math.atan2(d.y, d.z);
		yaw = Math.atan2(d.x, d.z);
		return new Vec3(pitch, yaw, roll);
	}
	
	public static Vec3 clamp(Vec3 in, double min, double max)
	{
		in.x = (in.x < min ? min : (in.x > max ? max : in.x));
		in.y = (in.y < min ? min : (in.y > max ? max : in.y));
		in.z = (in.z < min ? min : (in.z > max ? max : in.z));
		return in;
	}
	
	
}
